package com.olddriver.crm.web.controller;

import com.olddriver.crm.annotation.PermissionName;
import com.olddriver.crm.domain.AccidentBillItem;
import com.olddriver.crm.domain.Compensation;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.AccidentQueryObject;
import com.olddriver.crm.service.IAccidentBillItemService;
import com.olddriver.crm.service.ICompensationService;
import com.olddriver.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("accident")
public class AccidentBillItemController {
    @Autowired
    private IAccidentBillItemService accidentBillItemService;
    @Autowired
    private ICompensationService compensationService;


    @RequiresPermissions("accidentBillItem:list")
    @PermissionName("查看事故受理页面")
    @RequestMapping("list")
    public String list() {
        return "accidentBillItem/list";
    }

    @RequiresPermissions("accidentBillItem:view")
    @PermissionName("查看事故审核页面")
    @RequestMapping("auditView")
    public String auditView() {
        return "accidentBillItem/auditView";
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<AccidentBillItem> listAll() {
        return accidentBillItemService.listAll();
    }
    @ResponseBody
    @RequestMapping("query")
    @RequiresPermissions("accidentBillItem:query")
    @PermissionName("事故明细单列表")
    public PageResult query(AccidentQueryObject qo) {
        PageResult result = accidentBillItemService.query(qo);
        return result;
    }

    //审核查询
    @ResponseBody
    @RequestMapping("auditSelect")
    @RequiresPermissions("accidentBillItem:auditSelect")
    @PermissionName("审核状态表")
    public PageResult auditSelect(AccidentQueryObject qo) {
        PageResult result = accidentBillItemService.query(qo);
        return result;
    }

    //编辑事故资料
    @ResponseBody
    @RequestMapping("updateCaseState")
    @RequiresPermissions("accidentBillItem:updateCaseState")
    public JsonResult updateCaseState(AccidentBillItem accident){
        try {
            accidentBillItemService.updateCaseState(accident);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }
    /*//修改上报审批状态
    @ResponseBody
    @RequestMapping("changeAppearState")
    @RequiresPermissions("accidentBillItem:changeAppearState")
    public JsonResult changeAppearState(Long id){
        try {
            accidentBillItemService.changeAppearState(id);
            return JsonResult.success();
        }catch (Exception e){
            return JsonResult.faliure();
        }
    }*/
    //审核改变状态
    @ResponseBody
    @RequestMapping("changeAuditState")
    @RequiresPermissions("accidentBillItem:changeAuditState")
    public JsonResult changeAuditState(Long id){
        try {
            accidentBillItemService.changeAuditState(id);
            return JsonResult.success();
        }catch (Exception e){
            return JsonResult.faliure();
        }
    }

    @RequiresPermissions("accidentBillItem:reported")
    @PermissionName("上报审批")
    @ResponseBody
    @RequestMapping("reported")
    public JsonResult reported(Long accidentId){
        try {
            //查询赔损单
            Compensation compensation = compensationService.selectByPrimaryKey(accidentId);
            if (compensation == null){
                //为空返回错误信息
                return JsonResult.diyFaliure("赔损人信息不完整!");
            }else{
                //调用上报审批方法,修改状态
                accidentBillItemService.changeAppearState(accidentId);
                return JsonResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    //拒保
    @RequiresPermissions("accidentBillItem:declinature")
    @PermissionName("上报审批")
    @ResponseBody
    @RequestMapping("declinature")
    public JsonResult declinature(Long id){
        try {
            //accidentBillItemService.delete(id); 要修改
            accidentBillItemService.declinature(id);
            return JsonResult.diySuccess("拒保成功!");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }
    //保存赔损人信息
    @RequiresPermissions("accidentBillItem:saveCompesation")
    @PermissionName("上报审批")
    @ResponseBody
    @RequestMapping("saveCompesation")
    public JsonResult saveCompesation(Compensation compensation,Long accidentId){
        try {
            AccidentBillItem accident = accidentBillItemService.selectByPrimaryKey(accidentId);
            compensation.setAccident(accident);
            compensationService.insert(compensation);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }


}
